package com.stu.boke.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.stu.boke.entity.Notice;
import com.stu.boke.service.NoticeService;

@Controller
@RequestMapping(value = "/admin/notice")
public class NoticeController {

	@Autowired
	private NoticeService noticeService;
	
	@RequestMapping("")
	public ModelAndView indexNotice() {
		ModelAndView model = new ModelAndView();
		List<Notice> noticeList = noticeService.listNotice(null);
		model.addObject("noticeList", noticeList);
		model.setViewName("Admin/Notice/index");
		return model;
	}
	
	@RequestMapping(value = "/insertSubmit")
	public String insertSubmitNotice(Notice notice) {
		noticeService.insertSubmitNotice(notice);
		return "redirect:/admin/notice";
	}
	
	@RequestMapping(value = "/edit/{id}")
	public ModelAndView editNoticeView(@PathVariable("id") Integer id) {
		ModelAndView modelAndView = new ModelAndView();
		Notice notice = noticeService.getNoticeById(id);
		modelAndView.addObject("notice", notice);
		modelAndView.setViewName("Admin/Notice/edit");
		return modelAndView;
	}
	
	@RequestMapping(value = "/editSubmit")
	public String editNoticeSubmit(Notice notice) {
		noticeService.updateNotice(notice);
		return "redirect:/admin/notice";
	}
	
	@RequestMapping(value = "/insert")
	public ModelAndView insertArticle(Model model) {
		ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("Admin/Notice/insert");
        return modelAndView;
	}
	
	@RequestMapping(value = "/delete/{id}")
	public String deleteNotice(@PathVariable("id") Integer id) {
		noticeService.deleteNotice(id);
		return "redirect:/admin/notice";
	}
}
